package future;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CancelDemo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<String> future = executorService.submit(() -> {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                System.out.println("sleep " + i);
                Thread.sleep(1000L);
                sb.append(i);
            }
            return sb.toString();
        });
        Thread.sleep(3000L);
        future.cancel(true);
        executorService.shutdown();
    }
}
